// pages/2411Ayiliao-pages/inpatient/recharge.js
Page({
	data: {
		patientInfo: {
			name: '牛韦',
			inpatientNumber: '390090',
			balance: '¥60.00'
		},
		rechargeAmounts: [
			{ text: '500', value: 500 },
			{ text: '1000', value: 1000 },
			{ text: '2000', value: 2000 },
			{ text: '5000', value: 5000 },
			{ text: '10000', value: 10000 },
			{ text: '自定义', value: 'custom' }
		],
		selectedAmount: 500
	},

	onLoad(options) {
		// 从住院人信息页面传递的数据
		if (options.data) {
			try {
				const data = JSON.parse(decodeURIComponent(options.data))
				this.setData({
					'patientInfo.name': data.name || this.data.patientInfo.name,
					'patientInfo.inpatientNumber': data.inpatientNumber || this.data.patientInfo.inpatientNumber
				})
			} catch (e) {
				console.error('解析数据失败:', e)
			}
		}
	},

	selectAmount(e) {
		const amount = e.currentTarget.dataset.amount
		if (amount === 'custom') {
			// 自定义金额输入
			wx.showModal({
				title: '自定义金额',
				content: '请输入充值金额',
				editable: true,
				placeholderText: '请输入金额',
				success: (res) => {
					if (res.confirm && res.content) {
						const customAmount = parseFloat(res.content)
						if (customAmount > 0) {
							this.setData({
								selectedAmount: customAmount
							})
						} else {
							wx.showToast({
								title: '请输入有效金额',
								icon: 'none'
							})
						}
					}
				}
			})
		} else {
			this.setData({
				selectedAmount: amount
			})
		}
	},

	viewRechargeRecord() {
		wx.navigateTo({
			url: '/pages/2411Ayiliao-pages/recharge/inpatient'
		})
	},

	confirmRecharge() {
		if (this.data.selectedAmount === 'custom') {
			wx.showToast({
				title: '请选择充值金额',
				icon: 'none'
			})
			return
		}
		
		wx.showModal({
			title: '确认充值',
			content: `确认充值 ¥${this.data.selectedAmount} 吗？`,
			success: (res) => {
				if (res.confirm) {
					// 跳转到支付密码页面
					const paymentData = {
						amount: this.data.selectedAmount
					}
					wx.navigateTo({
						url: `/pages/2411Ayiliao-pages/inpatient/payment?data=${encodeURIComponent(JSON.stringify(paymentData))}`
					})
				}
			}
		})
	}
}) 